#!/usr/bin/env python
# coding:utf-8

import traceback


class AbstractConverter(object):

    def convert(self, row):
        pass

    def filter(self, row):
        pass


class RowConverter(AbstractConverter):

    # 数据源的schema
    table_schema = {
        'feature_dt_retention_user_action': 'mid,retention,like_count_1day,dislike_count_1day,tedium_count_1day,expose_count_1day,stay_time_sum_1day,share_count_1day,favor_count_1day,play_video_count_1day,play_video_dur_sum_1day,view_img_count_1day,view_img_dur_sum_1day,like_danmaku_count_1day,create_danmaku_count_1day,detail_post_count_1day,play_audio_count_1day,play_audio_dur_sum_1day,create_review_count_1day,reply_review_count_1day,play_review_video_count_1day,play_review_video_dur_sum_1day,view_review_img_count_1day,view_review_img_dur_sum_1day,play_review_audio_count_1day,play_review_audio_dur_sum_1day,subreviews_review_count_1day,like_rate_1day,dislike_rate_1day,tedium_rate_1day,share_rate_1day,favor_rate_1day,play_video_rate_1day,view_img_rate_1day,like_danmaku_rate_1day,create_danmaku_rate_1day,detail_post_rate_1day,play_audio_rate_1day,create_review_rate_1day,reply_review_rate_1day,play_review_video_rate_1day,view_review_img_rate_1day,play_review_audio_rate_1day,subreviews_review_rate_1day,dislike_like_rate_1day,tedium_like_rate_1day,share_like_rate_1day,favor_like_rate_1day,create_review_detail_rate_1day,reply_review_detail_rate_1day,play_review_video_detail_rate_1day,view_review_img_detail_rate_1day,play_review_audio_detail_rate_1day,stay_time_avg_1day,play_video_dur_avg_1day,view_img_dur_avg_1day,play_audio_dur_avg_1day,play_review_video_dur_avg_1day,view_review_img_dur_avg_1day,play_review_audio_dur_avg_1day,like_count_7day,dislike_count_7day,tedium_count_7day,expose_count_7day,stay_time_sum_7day,share_count_7day,favor_count_7day,play_video_count_7day,play_video_dur_sum_7day,view_img_count_7day,view_img_dur_sum_7day,like_danmaku_count_7day,create_danmaku_count_7day,detail_post_count_7day,play_audio_count_7day,play_audio_dur_sum_7day,create_review_count_7day,reply_review_count_7day,play_review_video_count_7day,play_review_video_dur_sum_7day,view_review_img_count_7day,view_review_img_dur_sum_7day,play_review_audio_count_7day,play_review_audio_dur_sum_7day,subreviews_review_count_7day,like_decay_7day,dislike_decay_7day,tedium_decay_7day,expose_decay_7day,stay_time_decay_7day,share_decay_7day,favor_decay_7day,play_video_decay_7day,play_video_dur_decay_7day,view_img_decay_7day,view_img_dur_decay_7day,like_danmaku_decay_7day,create_danmaku_decay_7day,detail_post_decay_7day,play_audio_decay_7day,play_audio_dur_decay_7day,create_review_decay_7day,reply_review_decay_7day,play_review_video_decay_7day,play_review_video_dur_decay_7day,view_review_img_decay_7day,view_review_img_dur_decay_7day,play_review_audio_decay_7day,play_review_audio_dur_decay_7day,subreviews_review_decay_7day,like_rate_7day,dislike_rate_7day,tedium_rate_7day,share_rate_7day,favor_rate_7day,play_video_rate_7day,view_img_rate_7day,like_danmaku_rate_7day,create_danmaku_rate_7day,detail_post_rate_7day,play_audio_rate_7day,create_review_rate_7day,reply_review_rate_7day,play_review_video_rate_7day,view_review_img_rate_7day,play_review_audio_rate_7day,subreviews_review_rate_7day,dislike_like_rate_7day,tedium_like_rate_7day,share_like_rate_7day,favor_like_rate_7day,create_review_detail_rate_7day,reply_review_detail_rate_7day,play_review_video_detail_rate_7day,view_review_img_detail_rate_7day,play_review_audio_detail_rate_7day,stay_time_avg_7day,play_video_dur_avg_7day,view_img_dur_avg_7day,play_audio_dur_avg_7day,play_review_video_dur_avg_7day,view_review_img_dur_avg_7day,play_review_audio_dur_avg_7day,like_count_30day,dislike_count_30day,tedium_count_30day,expose_count_30day,stay_time_sum_30day,share_count_30day,favor_count_30day,play_video_count_30day,play_video_dur_sum_30day,view_img_count_30day,view_img_dur_sum_30day,like_danmaku_count_30day,create_danmaku_count_30day,detail_post_count_30day,play_audio_count_30day,play_audio_dur_sum_30day,create_review_count_30day,reply_review_count_30day,play_review_video_count_30day,play_review_video_dur_sum_30day,view_review_img_count_30day,view_review_img_dur_sum_30day,play_review_audio_count_30day,play_review_audio_dur_sum_30day,subreviews_review_count_30day,like_decay_30day,dislike_decay_30day,tedium_decay_30day,expose_decay_30day,stay_time_decay_30day,share_decay_30day,favor_decay_30day,play_video_decay_30day,play_video_dur_decay_30day,view_img_decay_30day,view_img_dur_decay_30day,like_danmaku_decay_30day,create_danmaku_decay_30day,detail_post_decay_30day,play_audio_decay_30day,play_audio_dur_decay_30day,create_review_decay_30day,reply_review_decay_30day,play_review_video_decay_30day,play_review_video_dur_decay_30day,view_review_img_decay_30day,view_review_img_dur_decay_30day,play_review_audio_decay_30day,play_review_audio_dur_decay_30day,subreviews_review_decay_30day,like_rate_30day,dislike_rate_30day,tedium_rate_30day,share_rate_30day,favor_rate_30day,play_video_rate_30day,view_img_rate_30day,like_danmaku_rate_30day,create_danmaku_rate_30day,detail_post_rate_30day,play_audio_rate_30day,create_review_rate_30day,reply_review_rate_30day,play_review_video_rate_30day,view_review_img_rate_30day,play_review_audio_rate_30day,subreviews_review_rate_30day,dislike_like_rate_30day,tedium_like_rate_30day,share_like_rate_30day,favor_like_rate_30day,create_review_detail_rate_30day,reply_review_detail_rate_30day,play_review_video_detail_rate_30day,view_review_img_detail_rate_30day,play_review_audio_detail_rate_30day,stay_time_avg_30day,play_video_dur_avg_30day,view_img_dur_avg_30day,play_audio_dur_avg_30day,play_review_video_dur_avg_30day'.split(
            ','),
        'topic_dt_retention_user_action': 'mid,like_count_1day,dislike_count_1day,tedium_count_1day,expose_count_1day,stay_time_sum_1day,share_count_1day,favor_count_1day,play_video_count_1day,play_video_dur_sum_1day,view_img_count_1day,view_img_dur_sum_1day,like_danmaku_count_1day,create_danmaku_count_1day,detail_post_count_1day,play_audio_count_1day,play_audio_dur_sum_1day,create_review_count_1day,reply_review_count_1day,play_review_video_count_1day,play_review_video_dur_sum_1day,view_review_img_count_1day,view_review_img_dur_sum_1day,play_review_audio_count_1day,play_review_audio_dur_sum_1day,subreviews_review_count_1day,like_rate_1day,dislike_rate_1day,tedium_rate_1day,share_rate_1day,favor_rate_1day,play_video_rate_1day,view_img_rate_1day,like_danmaku_rate_1day,create_danmaku_rate_1day,detail_post_rate_1day,play_audio_rate_1day,create_review_rate_1day,reply_review_rate_1day,play_review_video_rate_1day,view_review_img_rate_1day,play_review_audio_rate_1day,subreviews_review_rate_1day,dislike_like_rate_1day,tedium_like_rate_1day,share_like_rate_1day,favor_like_rate_1day,create_review_detail_rate_1day,reply_review_detail_rate_1day,play_review_video_detail_rate_1day,view_review_img_detail_rate_1day,play_review_audio_detail_rate_1day,stay_time_avg_1day,play_video_dur_avg_1day,view_img_dur_avg_1day,play_audio_dur_avg_1day,play_review_video_dur_avg_1day,view_review_img_dur_avg_1day,play_review_audio_dur_avg_1day,like_count_7day,dislike_count_7day,tedium_count_7day,expose_count_7day,stay_time_sum_7day,share_count_7day,favor_count_7day,play_video_count_7day,play_video_dur_sum_7day,view_img_count_7day,view_img_dur_sum_7day,like_danmaku_count_7day,create_danmaku_count_7day,detail_post_count_7day,play_audio_count_7day,play_audio_dur_sum_7day,create_review_count_7day,reply_review_count_7day,play_review_video_count_7day,play_review_video_dur_sum_7day,view_review_img_count_7day,view_review_img_dur_sum_7day,play_review_audio_count_7day,play_review_audio_dur_sum_7day,subreviews_review_count_7day,like_decay_7day,dislike_decay_7day,tedium_decay_7day,expose_decay_7day,stay_time_decay_7day,share_decay_7day,favor_decay_7day,play_video_decay_7day,play_video_dur_decay_7day,view_img_decay_7day,view_img_dur_decay_7day,like_danmaku_decay_7day,create_danmaku_decay_7day,detail_post_decay_7day,play_audio_decay_7day,play_audio_dur_decay_7day,create_review_decay_7day,reply_review_decay_7day,play_review_video_decay_7day,play_review_video_dur_decay_7day,view_review_img_decay_7day,view_review_img_dur_decay_7day,play_review_audio_decay_7day,play_review_audio_dur_decay_7day,subreviews_review_decay_7day,like_rate_7day,dislike_rate_7day,tedium_rate_7day,share_rate_7day,favor_rate_7day,play_video_rate_7day,view_img_rate_7day,like_danmaku_rate_7day,create_danmaku_rate_7day,detail_post_rate_7day,play_audio_rate_7day,create_review_rate_7day,reply_review_rate_7day,play_review_video_rate_7day,view_review_img_rate_7day,play_review_audio_rate_7day,subreviews_review_rate_7day,dislike_like_rate_7day,tedium_like_rate_7day,share_like_rate_7day,favor_like_rate_7day,create_review_detail_rate_7day,reply_review_detail_rate_7day,play_review_video_detail_rate_7day,view_review_img_detail_rate_7day,play_review_audio_detail_rate_7day,stay_time_avg_7day,play_video_dur_avg_7day,view_img_dur_avg_7day,play_audio_dur_avg_7day,play_review_video_dur_avg_7day,view_review_img_dur_avg_7day,play_review_audio_dur_avg_7day,like_count_30day,dislike_count_30day,tedium_count_30day,expose_count_30day,stay_time_sum_30day,share_count_30day,favor_count_30day,play_video_count_30day,play_video_dur_sum_30day,view_img_count_30day,view_img_dur_sum_30day,like_danmaku_count_30day,create_danmaku_count_30day,detail_post_count_30day,play_audio_count_30day,play_audio_dur_sum_30day,create_review_count_30day,reply_review_count_30day,play_review_video_count_30day,play_review_video_dur_sum_30day,view_review_img_count_30day,view_review_img_dur_sum_30day,play_review_audio_count_30day,play_review_audio_dur_sum_30day,subreviews_review_count_30day,like_decay_30day,dislike_decay_30day,tedium_decay_30day,expose_decay_30day,stay_time_decay_30day,share_decay_30day,favor_decay_30day,play_video_decay_30day,play_video_dur_decay_30day,view_img_decay_30day,view_img_dur_decay_30day,like_danmaku_decay_30day,create_danmaku_decay_30day,detail_post_decay_30day,play_audio_decay_30day,play_audio_dur_decay_30day,create_review_decay_30day,reply_review_decay_30day,play_review_video_decay_30day,play_review_video_dur_decay_30day,view_review_img_decay_30day,view_review_img_dur_decay_30day,play_review_audio_decay_30day,play_review_audio_dur_decay_30day,subreviews_review_decay_30day,like_rate_30day,dislike_rate_30day,tedium_rate_30day,share_rate_30day,favor_rate_30day,play_video_rate_30day,view_img_rate_30day,like_danmaku_rate_30day,create_danmaku_rate_30day,detail_post_rate_30day,play_audio_rate_30day,create_review_rate_30day,reply_review_rate_30day,play_review_video_rate_30day,view_review_img_rate_30day,play_review_audio_rate_30day,subreviews_review_rate_30day,dislike_like_rate_30day,tedium_like_rate_30day,share_like_rate_30day,favor_like_rate_30day,create_review_detail_rate_30day,reply_review_detail_rate_30day,play_review_video_detail_rate_30day,view_review_img_detail_rate_30day,play_review_audio_detail_rate_30day,stay_time_avg_30day,play_video_dur_avg_30day,view_img_dur_avg_30day,play_audio_dur_avg_30day,play_review_video_dur_avg_30day'.split(
            ','),
        'feature_dt_ups_with_rt_30day_user': 'is_down,is_x,consume_duration,is_fav,is_forward,is_collect,clk,is_detailed,week,hour,minute,position,tab,net_env,channel,model,device_channel,device_model,pid,l1part_id,l2part_id,post_type,post_tid,img_sz,video_sz,dur,content_len,post_age,mid,user_gender,user_isreg,zy_age,reg_age,user_age,all_expose_count,like_count,dislike_count,tedium_count,expose_count,stay_time_sum,share_count,favor_count,play_video_count,play_video_dur_sum,view_img_count,view_img_dur_sum,like_danmaku_count,create_danmaku_count,detail_post_count,play_audio_count,play_audio_dur_sum,create_review_count,reply_review_count,play_review_video_count,play_review_video_dur_sum,view_review_img_count,view_review_img_dur_sum,subreviews_review_count,like_decay,dislike_decay,tedium_decay,expose_decay,stay_time_decay,share_decay,favor_decay,play_video_decay,play_video_dur_decay,view_img_decay,view_img_dur_decay,like_danmaku_decay,create_danmaku_decay,detail_post_decay,play_audio_decay,play_audio_dur_decay,create_review_decay,reply_review_decay,play_review_video_decay,play_review_video_dur_decay,view_review_img_decay,view_review_img_dur_decay,subreviews_review_decay,like_rate,dislike_rate,tedium_rate,share_rate,favor_rate,play_video_rate,view_img_rate,like_danmaku_rate,create_danmaku_rate,detail_post_rate,play_audio_rate,create_review_rate,reply_review_rate,play_review_video_rate,view_review_img_rate,subreviews_review_rate,dislike_like_rate,tedium_like_rate,share_like_rate,favor_like_rate,create_review_detail_rate,reply_review_detail_rate,play_review_video_detail_rate,view_review_img_detail_rate,stay_time_avg,play_video_dur_avg,view_img_dur_avg,play_audio_dur_avg,play_review_video_dur_avg,view_review_img_dur_avg,post_like_count,post_dislike_count,post_tedium_count,post_expose_count,post_stay_time_sum,post_share_count,post_favor_count,post_play_video_count,post_play_video_dur_sum,post_view_img_count,post_view_img_dur_sum,post_like_danmaku_count,post_create_danmaku_count,post_detail_post_count,post_play_audio_count,post_play_audio_dur_sum,post_create_review_count,post_reply_review_count,post_play_review_video_count,post_play_review_video_dur_sum,post_view_review_img_count,post_view_review_img_dur_sum,post_subreviews_review_count,post_like_decay,post_dislike_decay,post_tedium_decay,post_expose_decay,post_stay_time_decay,post_share_decay,post_favor_decay,post_play_video_decay,post_play_video_dur_decay,post_view_img_decay,post_view_img_dur_decay,post_like_danmaku_decay,post_create_danmaku_decay,post_detail_post_decay,post_play_audio_decay,post_play_audio_dur_decay,post_create_review_decay,post_reply_review_decay,post_play_review_video_decay,post_play_review_video_dur_decay,post_view_review_img_decay,post_view_review_img_dur_decay,post_subreviews_review_decay,post_like_rate,post_dislike_rate,post_tedium_rate,post_share_rate,post_favor_rate,post_play_video_rate,post_view_img_rate,post_like_danmaku_rate,post_create_danmaku_rate,post_detail_post_rate,post_play_audio_rate,post_create_review_rate,post_reply_review_rate,post_play_review_video_rate,post_view_review_img_rate,post_subreviews_review_rate,post_dislike_like_rate,post_tedium_like_rate,post_share_like_rate,post_favor_like_rate,post_create_review_detail_rate,post_reply_review_detail_rate,post_play_review_video_detail_rate,post_view_review_img_detail_rate,post_stay_time_avg,post_play_video_dur_avg,post_view_img_dur_avg,post_play_audio_dur_avg,post_play_review_video_dur_avg,post_view_review_img_dur_avg,pga_like_count,pga_dislike_count,pga_tedium_count,pga_expose_count,pga_stay_time_sum,pga_share_count,pga_favor_count,pga_play_video_count,pga_play_video_dur_sum,pga_view_img_count,pga_view_img_dur_sum,pga_like_danmaku_count,pga_create_danmaku_count,pga_detail_post_count,pga_play_audio_count,pga_play_audio_dur_sum,pga_create_review_count,pga_reply_review_count,pga_play_review_video_count,pga_play_review_video_dur_sum,pga_view_review_img_count,pga_view_review_img_dur_sum,pga_subreviews_review_count,pga_like_decay,pga_dislike_decay,pga_tedium_decay,pga_expose_decay,pga_stay_time_decay,pga_share_decay,pga_favor_decay,pga_play_video_decay,pga_play_video_dur_decay,pga_view_img_decay,pga_view_img_dur_decay,pga_like_danmaku_decay,pga_create_danmaku_decay,pga_detail_post_decay,pga_play_audio_decay,pga_play_audio_dur_decay,pga_create_review_decay,pga_reply_review_decay,pga_play_review_video_decay,pga_play_review_video_dur_decay,pga_view_review_img_decay,pga_view_review_img_dur_decay,pga_subreviews_review_decay,pga_like_rate,pga_dislike_rate,pga_tedium_rate,pga_share_rate,pga_favor_rate,pga_play_video_rate,pga_view_img_rate,pga_like_danmaku_rate,pga_create_danmaku_rate,pga_detail_post_rate,pga_play_audio_rate,pga_create_review_rate,pga_reply_review_rate,pga_play_review_video_rate,pga_view_review_img_rate,pga_subreviews_review_rate,pga_dislike_like_rate,pga_tedium_like_rate,pga_share_like_rate,pga_favor_like_rate,pga_create_review_detail_rate,pga_reply_review_detail_rate,pga_play_review_video_detail_rate,pga_view_review_img_detail_rate,pga_stay_time_avg,pga_play_video_dur_avg,pga_view_img_dur_avg,pga_play_audio_dur_avg,pga_play_review_video_dur_avg,pga_view_review_img_dur_avg,actstats,rt_user_part_like_count,rt_user_part_dislike_count,rt_user_part_tedium_count,rt_user_part_expose_count,rt_user_part_share_count,rt_user_part_favor_count,rt_user_part_play_video_count,rt_user_part_play_video_dur_sum,rt_user_part_view_img_count,rt_user_part_view_img_dur_sum,rt_user_part_like_danmaku_count,rt_user_part_create_danmaku_count,rt_user_part_detail_post_count,rt_user_part_audio_play_count,rt_user_part_audio_play_dur_sum,rt_user_part_reply_review_count,rt_user_part_create_review_count,rt_user_part_like_rate,rt_user_part_dislike_rate,rt_user_part_tedium_rate,rt_user_part_share_rate,rt_user_part_favor_rate,rt_user_part_play_video_rate,rt_user_part_view_img_rate,rt_user_part_like_danmaku_rate,rt_user_part_create_danmaku_rate,rt_user_part_detail_post_rate,rt_user_part_play_audio_rate,rt_user_part_create_review_rate,rt_user_part_reply_review_rate,rt_user_part_dislike_like_rate,rt_user_part_tedium_like_rate,rt_user_part_share_like_rate,rt_user_part_favor_like_rate,rt_user_part_create_review_detail_rate,rt_user_part_reply_review_detail_rate,rt_user_part_play_video_dur_avg,rt_user_part_view_img_dur_avg,rt_user_ptype_like_count,rt_user_ptype_dislike_count,rt_user_ptype_tedium_count,rt_user_ptype_expose_count,rt_user_ptype_share_count,rt_user_ptype_favor_count,rt_user_ptype_play_video_count,rt_user_ptype_play_video_dur_sum,rt_user_ptype_view_img_count,rt_user_ptype_view_img_dur_sum,rt_user_ptype_like_danmaku_count,rt_user_ptype_create_danmaku_count,rt_user_ptype_detail_post_count,rt_user_ptype_audio_play_count,rt_user_ptype_audio_play_dur_sum,rt_user_ptype_reply_review_count,rt_user_ptype_create_review_count,rt_user_ptype_like_rate,rt_user_ptype_dislike_rate,rt_user_ptype_tedium_rate,rt_user_ptype_share_rate,rt_user_ptype_favor_rate,rt_user_ptype_play_video_rate,rt_user_ptype_view_img_rate,rt_user_ptype_like_danmaku_rate,rt_user_ptype_create_danmaku_rate,rt_user_ptype_detail_post_rate,rt_user_ptype_play_audio_rate,rt_user_ptype_create_review_rate,rt_user_ptype_reply_review_rate,rt_user_ptype_dislike_like_rate,rt_user_ptype_tedium_like_rate,rt_user_ptype_share_like_rate,rt_user_ptype_favor_like_rate,rt_user_ptype_create_review_detail_rate,rt_user_ptype_reply_review_detail_rate,rt_user_ptype_play_video_dur_avg,rt_user_ptype_view_img_dur_avg'.split(
            ','),
    }

    # 特征列表
    feature_schema = {
        'attr_post_pga_upart': 'week,hour,minute,tab,net_env,channel,post_type,img_sz,video_sz,dur,content_len,post_age,user_gender,user_isreg,zy_age,reg_age,user_age,post_dislike_count,post_stay_time_sum,post_share_count,post_view_img_count,post_play_review_video_count,post_like_decay,post_stay_time_decay,post_play_video_decay,post_view_img_decay,post_create_review_decay,post_reply_review_decay,post_play_review_video_decay,post_subreviews_review_decay,post_like_rate,post_tedium_rate,post_favor_rate,post_play_video_rate,post_view_img_rate,post_play_review_video_rate,post_subreviews_review_rate,post_dislike_like_rate,post_share_like_rate,post_play_review_video_detail_rate,post_view_review_img_detail_rate,post_stay_time_avg,pga_expose_count,pga_view_img_count,pga_play_review_video_count,pga_subreviews_review_count,pga_like_decay,pga_tedium_decay,pga_stay_time_decay,pga_share_decay,pga_play_video_decay,pga_view_img_dur_decay,pga_like_rate,pga_tedium_rate,pga_share_rate,pga_favor_rate,pga_play_video_rate,pga_view_img_rate,pga_detail_post_rate,pga_play_review_video_rate,pga_view_review_img_rate,pga_subreviews_review_rate,pga_share_like_rate,pga_play_review_video_detail_rate,pga_view_review_img_detail_rate,pga_stay_time_avg,pga_play_video_dur_avg,pga_view_img_dur_avg,pga_play_review_video_dur_avg,pga_view_review_img_dur_avg,expose_count,stay_time_sum,view_img_dur_sum,play_review_video_dur_sum,view_review_img_dur_sum,subreviews_review_count,expose_decay,stay_time_decay,play_video_decay,play_video_dur_decay,view_img_decay,view_img_dur_decay,detail_post_decay,play_review_video_decay,view_review_img_decay,subreviews_review_decay,favor_rate,play_video_rate,view_img_rate,detail_post_rate,play_review_video_rate,view_review_img_rate,subreviews_review_rate,share_like_rate,create_review_detail_rate,play_review_video_detail_rate,view_review_img_detail_rate,stay_time_avg,play_video_dur_avg,view_img_dur_avg,play_review_video_dur_avg,view_review_img_dur_avg'.split(
            ','),
        'attr_post_upart_rtupart_rtuptype': 'week,hour,minute,tab,net_env,channel,post_type,img_sz,video_sz,dur,content_len,post_age,user_gender,user_isreg,zy_age,reg_age,user_age,post_dislike_count,post_stay_time_sum,post_share_count,post_view_img_count,post_play_review_video_count,post_like_decay,post_stay_time_decay,post_play_video_decay,post_view_img_decay,post_create_review_decay,post_reply_review_decay,post_play_review_video_decay,post_subreviews_review_decay,post_like_rate,post_tedium_rate,post_favor_rate,post_play_video_rate,post_view_img_rate,post_play_review_video_rate,post_subreviews_review_rate,post_dislike_like_rate,post_share_like_rate,post_play_review_video_detail_rate,post_view_review_img_detail_rate,post_stay_time_avg,expose_count,stay_time_sum,view_img_dur_sum,play_review_video_dur_sum,view_review_img_dur_sum,subreviews_review_count,expose_decay,stay_time_decay,play_video_decay,play_video_dur_decay,view_img_decay,view_img_dur_decay,detail_post_decay,play_review_video_decay,view_review_img_decay,subreviews_review_decay,favor_rate,play_video_rate,view_img_rate,detail_post_rate,play_review_video_rate,view_review_img_rate,subreviews_review_rate,share_like_rate,create_review_detail_rate,play_review_video_detail_rate,view_review_img_detail_rate,stay_time_avg,play_video_dur_avg,view_img_dur_avg,play_review_video_dur_avg,view_review_img_dur_avg,rt_user_ptype_tedium_count,rt_user_ptype_expose_count,rt_user_ptype_play_video_count,rt_user_ptype_play_video_dur_sum,rt_user_ptype_view_img_count,rt_user_ptype_view_img_dur_sum,rt_user_ptype_detail_post_count,rt_user_ptype_create_review_count,rt_user_ptype_like_rate,rt_user_ptype_dislike_rate,rt_user_ptype_share_rate,rt_user_ptype_play_video_rate,rt_user_ptype_view_img_rate,rt_user_ptype_like_danmaku_rate,rt_user_ptype_create_danmaku_rate,rt_user_ptype_detail_post_rate,rt_user_ptype_play_video_dur_avg,rt_user_ptype_view_img_dur_avg,rt_user_part_expose_count,rt_user_part_favor_count,rt_user_part_play_video_count,rt_user_part_view_img_count,rt_user_part_view_img_dur_sum,rt_user_part_detail_post_count,rt_user_part_like_rate,rt_user_part_play_video_rate,rt_user_part_view_img_rate,rt_user_part_like_danmaku_rate,rt_user_part_detail_post_rate,rt_user_part_play_video_dur_avg,rt_user_part_view_img_dur_avg'.split(
            ','),
        'attr_post_upart_rtupart_rtuptype_v2': 'week,hour,minute,tab,net_env,channel,post_type,img_sz,video_sz,dur,content_len,post_age,user_gender,user_isreg,zy_age,reg_age,user_age,post_stay_time_sum,post_share_count,post_view_img_count,post_play_review_video_count,post_like_decay,post_stay_time_decay,post_play_video_decay,post_view_img_decay,post_create_review_decay,post_reply_review_decay,post_play_review_video_decay,post_subreviews_review_decay,post_like_rate,post_tedium_rate,post_favor_rate,post_play_video_rate,post_view_img_rate,post_play_review_video_rate,post_subreviews_review_rate,post_dislike_like_rate,post_share_like_rate,post_play_review_video_detail_rate,post_view_review_img_detail_rate,post_stay_time_avg,expose_count,stay_time_sum,view_img_dur_sum,play_review_video_dur_sum,view_review_img_dur_sum,subreviews_review_count,expose_decay,stay_time_decay,play_video_decay,play_video_dur_decay,view_img_decay,detail_post_decay,play_review_video_decay,subreviews_review_decay,favor_rate,play_video_rate,view_img_rate,detail_post_rate,play_review_video_rate,view_review_img_rate,subreviews_review_rate,share_like_rate,create_review_detail_rate,stay_time_avg,play_video_dur_avg,view_img_dur_avg,rt_user_ptype_expose_count,rt_user_ptype_play_video_count,rt_user_ptype_play_video_dur_sum,rt_user_ptype_view_img_count,rt_user_ptype_view_img_dur_sum,rt_user_ptype_detail_post_count,rt_user_ptype_create_review_count,rt_user_ptype_dislike_rate,rt_user_ptype_play_video_rate,rt_user_ptype_view_img_rate,rt_user_ptype_create_danmaku_rate,rt_user_ptype_detail_post_rate,rt_user_ptype_play_video_dur_avg,rt_user_ptype_view_img_dur_avg,rt_user_part_expose_count,rt_user_part_favor_count,rt_user_part_play_video_count,rt_user_part_view_img_count,rt_user_part_view_img_dur_sum,rt_user_part_detail_post_count,rt_user_part_play_video_rate,rt_user_part_view_img_rate,rt_user_part_like_danmaku_rate,rt_user_part_play_video_dur_avg'.split(
            ','),
    }

    # 离散特征特征列表
    category_schema = {
        'tab': ['0', '1', '2', '3'],
        'net_env': ['0', '1', '2', '4', '9', '100', '99'],
        'post_type': ['0', '1', '2', '3', '4', '5', '6'],
        'user_gender': ['0', '1', '2', '3'],
        'week': ['0', '1', '2', '3', '4', '5', '6'],
        'hour': ['0', '1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23'],
        'channel': ['other','appstore','oppo','vivo','huawei','xiaomi','yingyongbao','meizu','yybtf','baidu','qihu','ppzhushou','me','samsung','zuiyou','aliyun','sougou','chuizi','pinzhuan1','yyb_share'],
        'user_age': ['0', '1', '2', '3', '4'],
    }

    def __init__(self, table_name, label_name, features_key, feature_map_file="", zy_age_threshold=0):
        self.zy_age_threshold = zy_age_threshold
        self.zy_age_index = -1
        self.label_index, self.features_schema_index, self.category_map, self.low_threshold = self.parse_schema(
            table_name, label_name, features_key, feature_map_file)

    # 根据数据源表名，目标、特征列表，得到数据转换所需信息
    def parse_schema(self, table_name, label_name, features_key, feature_map_file=""):
        schema = self.table_schema[table_name]
        label_index = schema.index(label_name)
        if 'zy_age' in schema:
            self.zy_age_index = schema.index('zy_age')
        features_schema_index = []
        features_schema_map = {}
        category_map = {}
        feature_list = self.feature_schema[features_key]
        feature_map_str = ""
        index = 0
        low_threshold = -1
        if label_name == "consume_duration":
            low_threshold = 2
        for feature in feature_list:
            schema_index = schema.index(feature)
            features_schema_index.append(schema_index)
            features_schema_map[feature] = schema_index
            if feature in self.category_schema:
                category_map[schema_index] = self.category_schema[feature]
                for category_feature_value in self.category_schema[feature]:
                    feature_map_str += "%d\t%s-%s\ti\n" % (index, feature, category_feature_value)
                    index += 1
            else:
                feature_map_str += "%d\t%s\tq\n" % (index, feature)
                index += 1
        if len(feature_map_file):
            with open(feature_map_file, "w") as out_file:
                out_file.writelines(feature_map_str)
        print("========================\n\tlabel_index: %d\n\tfeatures_schema_index: %s\n\tfeatures_schema_map: %s\n\tcategory_map: %s" % (
            label_index, features_schema_index, features_schema_map, category_map))
        return label_index, features_schema_index, category_map, low_threshold

    # 将csv的一行数据转成libsvm格式
    # 1. 支持特征列选择。需要提供libsvm中数据列与csv中数据列的对应关系
    # 2. 支持onehot编码。需要libsvm中离散特征的onehot字典
    # 3. 支持行过滤。需要制定过滤列，
    def convert(self, row):
        try:
            result_line = ""
            if self.zy_age_index > 0 and self.zy_age_threshold > 0 and (int(row[self.zy_age_index]) > self.zy_age_threshold):
                return result_line
            if self.low_threshold >= 0:
                label = int(row[self.label_index])
                if label > 1800 or label < self.low_threshold:
                    return ""
                label = min(label, 420)
                result_line += str(label)
            else:
                result_line += row[self.label_index]
            index = 0
            for schema_index in self.features_schema_index:
                if schema_index in self.category_map:
                    # 离散特征
                    category_values = self.category_map[schema_index]
                    value = row[schema_index]
                    if value in category_values:
                        cate_index = category_values.index(value)
                        result_line += ' %d:1' % (index + cate_index)
                    else:
                        if category_values[0] == 'other':
                            result_line += ' %d:1' % index
                        else:
                            print("Unknown value:%s, for category_map:%s." % (value, category_values))
                    index += len(category_values)
                else:
                    # 连续特征
                    value = row[schema_index]
                    if value != '' and value != '0' and value != '0.0':
                        result_line += ' %d:%s' % (index, value)
                    index += 1
            result_line += "\n"
            return result_line
        except Exception as e:
            print("row：%s\nlabel_index: %d\n%s" % (row, self.label_index, e))
            traceback.print_exc()

